<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function renderUser() {
        while (true) {
          let userName = prompt('请输入用户名')
          let password = prompt('请输入密码')

          let loginList = [
            { userName: 'admin', password: 'admin321' },
            { userName: 'zhangsan', password: 'zs321' },
            { userName: 'lisi', password: 'ls321' },
            { userName: 'yyqx', password: 'yyqx321' },
          ]
          let userInfo = {
            token1: {
              city: '杭州',
              money: '1000w',
              sex: '女',
              userName: 'admin',
              password: 'admin321',
            },
            token2: {
              city: '北京',
              money: '1100w',
              sex: '女',
              userName: 'zhangsan',
              password: 'zs321',
            },
            token3: {
              city: '北京',
              money: '800w',
              sex: '男',
              userName: 'lisi',
              password: 'ls321',
            },
            token4: {
              city: '天津',
              money: '600w',
              sex: '男',
              userName: 'yyqx',
              password: 'yyqx321',
            },
          }
          for (let i = 0; i < loginList.length; i++) {
            if (
              userName === loginList[i].userName &&
              password === loginList[i].password
            ) {
              // for(let key in userInfo){
              // if(index === key){
              let index = 'token' + Math.floor(Math.random() * 4+1) 
              alert(
                `
              登录成功 ,具体用户信息:
              city :${userInfo[index].city}
              money:${userInfo[index].money}
              sex:${userInfo[index].sex}
              userName:${userInfo[index].userName}              
              `
              )
              return
              // }
              // break
              // }
            } else {
              let choose = +prompt(
                `
              用户不存在
              1. 注册 
              2.直接退出
              `
              )
              if (choose === 1) {
                let register = prompt('请注册用户名')
                let password = prompt('请输入密码')
                loginList.push(choose)
                // alert('注册成功')
                let obj = {
                  userName: register,
                  password: password,
                }
                loginList.splice(0, 0, obj)
                // console.log(loginList);
                 userName = prompt('请输入用户名')
                 password = prompt('请输入密码')
                for (let i = 0; i < loginList.length; i++) {
                  if (
                    userName === loginList[i].userName &&
                    password === loginList[i].password
                  ) {
                    let index = 'token' + Math.floor(Math.random() * 4+1)
                    return  alert(`
                    登录成功 ,具体用户信息:
                    city :${userInfo[index].city}
                    money:${userInfo[index].money}
                    sex:${userInfo[index].sex}
                    userName:${userInfo[index].userName}                   
              `) 
                               
                  }                  
                }
                return  alert('没机会了')
              
              } else {
                if (choose === 2  ) {
                  alert('直接退出')
                }
                return
              }
            }
          }
        }
      }
      renderUser()
    </script>
  </body>
</html>
